package com.example;

import com.example.bookmarks.xmlmapper.BookmarkH3;
import com.example.bookmarks.xmlmapper.BookmarkLink;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        xmlMapper = new XmlMapper();
        xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);


        // 读取HTML文件并检查每行是否以ABC开头
        String filePath = "bookmarks-process/src/main/resources/bookmarks/bookmarks_2025_10_19.html";
        lineProces(filePath);
    }


    // 创建XML映射器
    static XmlMapper xmlMapper = null;

    static HashSet<String> urls = new HashSet<>();

    /**
     * 逐行读取文件并检查每行是否以ABC开头
     *
     * @param filePath 文件路径
     */
    private static void lineProces(String filePath) {
        String outputFilePath = "bookmarks-process/src/main/resources/bookmarks/processed_lines.html";

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter(outputFilePath));
            String line;
//            int lineNumber = 0;

            while ((line = reader.readLine()) != null) {
//                lineNumber++;
                line = line.trim();
                if (!line.startsWith("<DT><A")) {
                    writer.write(line);
//                    writer.write(System.lineSeparator());
                    writer.newLine();
                    continue;
                }

                if (urls.contains(line)) {
                    continue;
                } else {
                    urls.add(line);
                    System.out.println(line);
                    writer.write(line);
//                    writer.write(System.lineSeparator());
                    writer.newLine();
                }
//                System.out.println(line);
//                lineProces2(line);
            }
            writer.close();
//            System.out.println("文件读取完成，总共检查了" + lineNumber + "行");
        } catch (IOException e) {
            System.err.println("读取文件时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void lineProces2(String line) {
        try {
            if (line.startsWith("<H3")) {
                BookmarkH3 h3 = xmlMapper.readValue(line, BookmarkH3.class);

            } else if (line.startsWith("<A")) {
                BookmarkLink link = xmlMapper.readValue(line, BookmarkLink.class);
                System.out.println(xmlMapper.writeValueAsString(link));
            }

        } catch (Exception e) {
            System.out.println("line: " + line);
            throw new RuntimeException(e);
        }
    }


}